/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.orderoid.ejb.facade;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.orderoid.ejb.domain.Commande;
import org.orderoid.ejb.domain.Employe;
import org.orderoid.ejb.domain.Restaurant;
import org.orderoid.ejb.domain.StatutCommande;

/**
 *
 * @author Dahlia
 */
@Stateless
public class CommandeFacade extends AbstractFacade<Commande> {

    @PersistenceContext(unitName = "Orderoid-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public CommandeFacade() {
        super(Commande.class);
    }

    /**
     * Liste les commandes d'un restaurant
     *
     * @param idRestaurant L'identifiant du restaurant
     * @return La liste des commandes
     */
    public List<Commande> listerCommandes(Integer idRestaurant) {
        // Restaurant r = e.getIdRestaurant();
        // String strGetIdRestaurantFromClient = "(SELECT t.id_restaurant FROM employe t WHERE t.nom = '" + e.getNom() + "')";

        // Attention, c.idRestaurant retourne un restaurant et pas son id !

        Query query = em.createQuery("SELECT c FROM Commande c WHERE c.idStatut.idStatut <> 3 AND c.idStatut.idStatut <> 10 AND c.idRestaurant.idRestaurant = :idResto");
        query.setParameter("idResto", idRestaurant);

        List<Commande> commandes = query.getResultList();


        return commandes;
    }
    
    /**
     * Liste les commandes d'un restaurant
     *
     * @param idClient L'identifiant du restaurant
     * @return La liste des commandes
     */
    public List<Commande> listerCommandesByClient(Integer idClient){
        
        Query query = em.createQuery("SELECT c FROM Commande c WHERE c.idClient.idClient = :idClient"
                + " ORDER BY c.dateCommande DESC");
        query.setParameter("idClient", idClient);

        List<Commande> commandes = query.getResultList();
        
        return commandes;
    }

    public void mettreAJourStatut(Commande c) {

        edit(c);
//        StatutCommande currentStatut = c.getIdStatut();
//        String updateCommande = "UPDATE :c AS co SET co.idStatutCommande = 2";
//        Query q = em.createQuery(updateCommande);
//        q.setParameter("s", successeur.getIdStatut());
//         q.setParameter("c", c);
//        
//        q.executeUpdate();
    }

    public List<Commande> listerCommandesTermines(Integer idRestaurant) {
        Query query = em.createQuery("SELECT c FROM Commande c WHERE (c.idStatut.idStatut = 3 OR c.idStatut.idStatut = 10)"
                + " AND c.idRestaurant.idRestaurant = :idResto"
                + " AND c.dateCommande >= :dateJour");
        query.setParameter("idResto", idRestaurant);
        Date maDate = new Date();
        maDate.setHours(0);
        maDate.setMinutes(0);
        maDate.setSeconds(0);
        query.setParameter("dateJour", maDate);

        List<Commande> commandes = query.getResultList();


        return commandes;
    }

    // Statistiques
    public Object getTotalCommandes(Integer idRestaurant) {
        Query query = em.createQuery("SELECT count(c) FROM Commande c WHERE c.dateCommande >= :dateJour AND c.dateCommande <= :dateFin "
                + "AND c.idRestaurant.idRestaurant = :idResto");
        query.setParameter("idResto", idRestaurant);
        Date maDate = new Date();
        maDate.setHours(0);
        maDate.setMinutes(0);
        maDate.setSeconds(0);
        query.setParameter("dateJour", maDate);

        Date maDateFin = new Date();
        maDateFin.setHours(23);
        maDateFin.setMinutes(59);
        maDateFin.setSeconds(59);
        query.setParameter("dateFin", maDateFin);

        Object nbcommandes = query.getSingleResult();

        return nbcommandes;
    }

    public Object getTotalCommandesEnCours(Integer idRestaurant) {
        Query query = em.createQuery("SELECT count(c) FROM Commande c WHERE c.dateCommande >= :dateJour "
                + "AND c.dateCommande <= :dateFin "
                + "AND c.idStatut.idStatut <> 3 "
                + "AND c.idStatut.idStatut <> 10 "
                + "AND c.idRestaurant.idRestaurant = :idResto");

        query.setParameter("idResto", idRestaurant);
        Date maDate = new Date();
        maDate.setHours(0);
        maDate.setMinutes(0);
        maDate.setSeconds(0);
        query.setParameter("dateJour", maDate);

        Date maDateFin = new Date();
        maDateFin.setHours(23);
        maDateFin.setMinutes(59);
        maDateFin.setSeconds(59);
        query.setParameter("dateFin", maDateFin);

        Object nbcommandes = query.getSingleResult();

        return nbcommandes;
    }

    public Object getTotalCommandesFinis(Integer idRestaurant) {
        Query query = em.createQuery("SELECT count(c) FROM Commande c WHERE c.dateCommande >= :dateJour "
                + "AND c.dateCommande <= :dateFin "
                + "AND (c.idStatut.idStatut = 3 OR c.idStatut.idStatut = 10) "
                + "AND c.idRestaurant.idRestaurant = :idResto");

        query.setParameter("idResto", idRestaurant);
        Date maDate = new Date();
        maDate.setHours(0);
        maDate.setMinutes(0);
        maDate.setSeconds(0);
        query.setParameter("dateJour", maDate);

        Date maDateFin = new Date();
        maDateFin.setHours(23);
        maDateFin.setMinutes(59);
        maDateFin.setSeconds(59);
        query.setParameter("dateFin", maDateFin);

        Object nbcommandes = query.getSingleResult();

        return nbcommandes;
    }

    public Object getCAeffectif(Integer idRestaurant) {
        Query query = em.createQuery("SELECT sum(c.montant) FROM Commande c WHERE c.dateCommande >= :dateJour "
                + "AND c.dateCommande <= :dateFin "
                + "AND c.idStatut.idStatut = 3 "
                + "AND c.idRestaurant.idRestaurant = :idResto");

        query.setParameter("idResto", idRestaurant);
        Date maDate = new Date();
        maDate.setHours(0);
        maDate.setMinutes(0);
        maDate.setSeconds(0);
        query.setParameter("dateJour", maDate);

        Date maDateFin = new Date();
        maDateFin.setHours(23);
        maDateFin.setMinutes(59);
        maDateFin.setSeconds(59);
        query.setParameter("dateFin", maDateFin);

        Object caeffectif = query.getSingleResult();

        return caeffectif;
    }

    public Object getCAprevisionnel(Integer idRestaurant) {
        Query query = em.createQuery("SELECT sum(c.montant) FROM Commande c WHERE c.dateCommande >= :dateJour "
                + "AND c.dateCommande <= :dateFin "
                + "AND c.idStatut.idStatut <> 3 "
                + "AND c.idStatut.idStatut <> 10 "
                + "AND c.idRestaurant.idRestaurant = :idResto");

        query.setParameter("idResto", idRestaurant);
        Date maDate = new Date();
        maDate.setHours(0);
        maDate.setMinutes(0);
        maDate.setSeconds(0);
        query.setParameter("dateJour", maDate);

        Date maDateFin = new Date();
        maDateFin.setHours(23);
        maDateFin.setMinutes(59);
        maDateFin.setSeconds(59);
        query.setParameter("dateFin", maDateFin);

        Object caprevisionnel = query.getSingleResult();

        return caprevisionnel;
    }

    public Object getCAtotal(Integer idRestaurant) {
        Query query = em.createQuery("SELECT sum(c.montant) FROM Commande c WHERE c.dateCommande >= :dateJour "
                + "AND c.dateCommande <= :dateFin "
                + "AND c.idStatut.idStatut <> 10 "
                + "AND c.idRestaurant.idRestaurant = :idResto");

        query.setParameter("idResto", idRestaurant);
        Date maDate = new Date();
        maDate.setHours(0);
        maDate.setMinutes(0);
        maDate.setSeconds(0);
        query.setParameter("dateJour", maDate);

        Date maDateFin = new Date();
        maDateFin.setHours(23);
        maDateFin.setMinutes(59);
        maDateFin.setSeconds(59);
        query.setParameter("dateFin", maDateFin);

        Object totalca = query.getSingleResult();

        return totalca;
    }
}
